package com.woke.httpupload;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Environment;
import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 作者 ： 暗网
 * 创建时间：2017/3/7
 * 备注：
 */

public class FileProviderUtils {
	public static File createImageFile(Context context) {
		if (context == null) {
			throw new RuntimeException ("Context is null");
		}
//		String timeStamp = new SimpleDateFormat ("yyyyMMdd_HHmmss").format (new Date ());
//		String imageFileName = "JPEG_" + timeStamp + "_";
//		File storageDir = context.getExternalCacheDir ();//外置SD卡
//		File image = File.createTempFile (imageFileName, ".jpg", storageDir);
//
//		return image;
		String timeStamp = new SimpleDateFormat ("yyyyMMdd_HHmmss_SSS").format (new Date ());
		String imageFileName = "JPEG_" + timeStamp + ".jpeg";

		return new File (getCacheDir (context), imageFileName);
	}

	/**
	 * 获取文件保存路径
	 *
	 * @param context
	 * @return
	 */
	private static File getCacheDir(Context context) {

		if (Environment.getExternalStorageState ().equals (Environment.MEDIA_MOUNTED)) {
			File file = context.getExternalCacheDir ();
			if (file != null) {

				Log.e ("PATH", "SDPATH==1111" + file.getAbsolutePath ().toString ());
				return file;
			} else {
				String path = Environment.getExternalStorageDirectory ().getPath ();
				Log.e ("PATH", "SDPATH==222" + path);
				return new File (path);
			}
		} else {
			File file = context.getCacheDir ();
			Log.e ("PATH", "getCacheDir()==" + file.getPath ().toString ());
			return file;
		}
	}

	public static File createCacheImageFile(Context context) throws IOException {
		if (context == null) {
			throw new RuntimeException ("Context is null");
		}
		String timeStamp = new SimpleDateFormat ("yyyyMMdd_HHmmss").format (new Date ());
		String imageFileName = "JPEG_" + timeStamp + "_";
		File storageDir = context.getFilesDir ();//
//		File image = File.createTempFile (imageFileName, ".jpg", storageDir);
		File image = new File (storageDir, imageFileName + "." + Bitmap.CompressFormat.JPEG.toString ());
		return image;
	}


	public static File createCacheHtmlFile(Context context) throws IOException {
		if (context == null) {
			throw new RuntimeException ("Context is null");
		}

		String timeStamp = new SimpleDateFormat ("yyyyMMdd_HHmmss_SSS").format (new Date ());
		String imageFileName = "unionpay" + timeStamp;
//		File storageDir = context.getFilesDir ();//
//		File imageFile = File.createTempFile (imageFileName, ".html", storageDir);
		File image = new File (getCacheDir (context), imageFileName + ".html");
		return image;
	}
}
